# encoding:utf-8
from apk_utg import apk_utg
from chk_result import chk_result


class checker(object):
    def __init__(self, path):
        self.utg = apk_utg(path)
        self.result = chk_result()
        # print(self.utg.ad_view)

    def check_all(self):
        self.result.app_package = self.utg.app_package
        # self.result.ad_node = [i for i in self.utg.ad_view_full.items()].extend(
        #     [i for i in self.utg.ad_view_small.items()]).extend([i for i in self.utg.ad_view_banner.items()])
        self.result.set_result(1, self.rules4type1())
        self.result.set_result(2, self.rules4type2())
        self.result.set_result(3, self.rules4type3())
        self.result.set_result(4, self.rules4type4())
        self.result.set_result(5, self.rules4type5())
        self.result.set_result(6, self.rules4type6())
        self.result.to_string()

    def is_node_main_act(self, node):
        if self.utg.fnode_node_act[node] in self.utg.app_main_activity:
            return True
        else:
            return False

    def is_node_contain_ad(self, node):
        if node in self.utg.ad_view_full.keys() or node in self.utg.ad_view_small.keys() \
                or node in self.utg.ad_view_banner.keys():
            return True
        else:
            return False

    def is_node_contain_ad_ecp_banner(self, node):
        if node in self.utg.ad_view_full.keys() or node in self.utg.ad_view_small.keys():
            return True
        else:
            return False

    def is_node_small_ad(self, node):
        if node in self.utg.ad_view_small.keys():
            return True
        else:
            return False

    def is_node_banner_ad(self, node):
        if node in self.utg.ad_view_banner.keys():
            return True
        else:
            return False

    def is_node_fullsreen_ad(self, node):
        if node in self.utg.ad_view_full.keys():
            return True
        else:
            return False

    def rules4type6(self):
        # 首页即全屏广告
        # 首页->点back->广告
        for no in self.utg.from_AMST_to.values():
            if self.is_node_fullsreen_ad(no) or self.is_node_small_ad(no):
                return no
        # if self.is_node_fullsreen_ad(self.utg.first_node):
        #     return self.utg.first_node
        # for eg in self.utg.edges:
        #     if self.is_node_main_act(eg['from']):
        #         for ev in eg['events']:
        #             if 'BACK' in ev['event_str']:
        #                 if self.is_node_contain_ad(eg['to']):
        #                     return eg['to']
        for no in self.utg.nodes:
            if self.is_node_main_act(no['state_str']):
                try:
                    # if self.is_node_contain_ad(self.utg.from_BACK_to[no['state_str']]):
                    #     return self.utg.from_BACK_to[no['state_str']]
                    for i in self.utg.from_BACK_to[no['state_str']]:
                        if self.is_node_fullsreen_ad(i) or self.is_node_small_ad(i):
                            return i
                except:
                    continue
        return False

    def rules4type5(self):
        # 检查全屏广告页面数量-结构不相同的数量
        # FULL,SMALL
        structure_str_list = []
        for no in self.utg.ad_view_full.keys():
            structure_str_list.append(self.utg.fnode_str_structure[no])
        # for no in self.utg.ad_view_small.keys():
        #     structure_str_list.append(self.utg.fnode_str_structure[no])
        structure_str_list = set(structure_str_list)
        # print(len(self.utg.ad_view_full))
        # print(len(structure_str_list))
        if len(structure_str_list) > 3:
            return True
        else:
            return False

    def rules4type4(self):
        # activity非app,存在广告
        # FULL or SMALL
        # 全屏广告 BACK 非应用界面
        # for no in self.utg.nodes:
        #     if no['package'] != self.utg.app_package:
        #         if self.is_node_contain_ad(no['state_str']):
        #             return no['state_str']
        for full in self.utg.ad_view_full.keys():
            try:
                if self.utg.fnode_str_info[self.utg.from_BACK_to[full][0]]['package'] != self.utg.app_package:
                    return self.utg.from_BACK_to[full][0]
            except:
                continue
        return False

    def rules4type3(self):
        # UNKNOWN
        pass

    def rules4type2(self):
        # 广告界面，BACK，出现Dialog
        # FULL,SMALL CENTER
        for full in self.utg.ad_view_full.keys():
            try:
                if self.utg.from_BACK_to[full][0] in self.utg.dialog:
                    return self.utg.from_BACK_to[full][0]
            except:
                continue
        return False

    def rules4type1(self):
        # SMALL 广告与任何控件有重叠
        # SMALL
        def overlap(Ax1, Ay1, Ax2, Ay2, Bx1, By1, Bx2, By2):
            if (Ax2 > Bx1 and Bx2 > Ax1 and Ay2 > By1 and By2 > Ay1):
                return True
            return False

        for sm, bounds in self.utg.ad_view_small.items():
            Ax1, Ay1, Ax2, Ay2 = bounds[0][0], bounds[0][1], bounds[1][0], \
                                 bounds[1][1]
            for view in self.utg.node_state[sm]['views']:
                Bx1, By1, Bx2, By2 = view['bounds'][0][0], view['bounds'][0][1], view['bounds'][1][0], \
                                     view['bounds'][1][1]
                if view['class'] == 'android.widget.ImageView' or view['class'] == 'android.widget.Button' or view[
                    'class'] == 'android.widget.ImageButton' or view['class'] == 'android.widget.TextView':
                    if overlap(Ax1, Ay1, Ax2, Ay2, Bx1, By1, Bx2, By2):
                        return sm
        return False
